define(["app","js/login/loginView","js/home/homeController","js/init", 'js/utils','js/util/socketUtil','js/util/toast'], 
	function(app, loginView,hc,base,Utils,socketUtil,toast) {
	
	
	var bindings = [{
		element: '.get-storage-data',
		event: 'click',
		handler: login
	}];

	function init(query){	
		
		loginView.render({			
			bindings: bindings
		});
	}

	function login() {
		
		if(!app.mainView){
			app = app.getApp();
		}
		
		
		var storedData = {
			loginname : $('#loginname').val(),
			password:$('#password').val()
		};
				
			  
		if(storedData && storedData.loginname) {

			//获取密钥
			//btnEvent.getKeys();
			//var encrypt = new jsencrypt.JSEncrypt();
			//encrypt.setPublicKey(pk.p);

			//var _p = {
				//userid : storedData.userid,
				//password:storedData.password
			//}
			//加密密码
			//var encrypted = encrypt.encrypt(JSON.stringify(_p));			
			
			app.f7.showPreloader('login...');
			
			var _param = {
				url	: base.base + base.login,
				data: storedData,
				success:function(data){

					if(data.retStatus ==='success'){
						
						app.f7.showNavbar();
						//初始化socket
						socketUtil.init(data.userid)

						app.setUser(data)					
						
						app.f7.closeModal();

						
						var cnt = 0;
						
						

									
						//初始化首页
						hc.init();
						//检查更新

						setTimeout(function(){							
							Utils.checkUpdate(app);
						},5000);
											
						
						
						
						//alert('login success...');
						//app.mainView.loadPage('patient.html',true);
						//Router.load('patient'); 
						
					}else{
						app.f7.hidePreloader();
						toast.show({msg:"用户名或密码错误"})										
						
					}
				}
				,
				error:function(){
					app.f7.hidePreloader();
					toast.show({msg:"登录失败，请再次尝试，如问题依旧，请联系客服进行处理"});				
					
				}
			}
			$.ajax(_param)
			//alert(JSON.stringify(storedData));
			}
		else {
			toast.show({msg:"用户名不能为空"})
		}
		
		
	}

	return {
		init: init
	};
});